package config

import (
	"fmt"
	"gopkg.in/ini.v1"
	"log"
	"time"
)

var (
	AppModel string

	DbDns string

	HttpPort         string
	HttpReadOutTime  time.Duration
	HttpWriteOutTime time.Duration
)

func init() {
	config, err := ini.Load("./config/conf.ini")
	if err != nil {
		log.Fatalf("加载配置文件失败 err:=%s", err)
	}

	AppModel = config.Section("").Key("app_model").MustString("debug") // 读取默认节点， 按key读取
	initServer(config)
	initMysql(config)
}

func initServer(config *ini.File) {
	server, err := config.GetSection("server")
	if err != nil {
		log.Fatalln("加载server服务配置失败")
	}
	HttpPort = server.Key("httpPort").MustString("8080")
	HttpReadOutTime = time.Duration(server.Key("httpReadOutTime").MustInt(60)) * time.Second
	HttpWriteOutTime = time.Duration(server.Key("httpWriteOutTime").MustInt(60)) * time.Second
}

func initMysql(config *ini.File) {
	mysql, err := config.GetSection("mysql")
	if err != nil {
		log.Fatalln("加载msyql配置失败")
	}

	user := mysql.Key("user").String()
	ip := mysql.Key("ip").MustString("127.0.0.1:3306")
	pwd := mysql.Key("pwd").MustString("123456")
	database := mysql.Key("database").MustString("test")

	DbDns = fmt.Sprintf("%s:%s@tcp(%s)/%s", user, pwd, ip, database)
}
